package com.linkoog.devtools.code_generator.wizard.generate.database.table.panel.portion;

import com.linkoog.devtools.code_generator.database.domain.TableInfo;
import com.linkoog.devtools.code_generator.wizard.generate.database.table.custom.DbtableInfoJTable;
import com.linkoog.devtools.code_generator.util.StringUtil;
//import com.intellij.ui.SearchTextField;
import com.intellij.util.ui.JBUI;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.function.Consumer;

public class DbtableLeftPanelBuilder {

    private JPanel myMainPanel;

    private JPanel searchPanel;
    private SearchTextField filterTextField;

    private JPanel dbtablePanel;
    private JScrollPane dbtableScrollPanel;
    private JTable dbtableJTable;



    private List<TableInfo> tableInfoList;

    private Consumer<TableInfo> dbtableClickConsumer;


    public DbtableLeftPanelBuilder() {
        myMainPanel.setMinimumSize(JBUI.size(200, -1));
        myMainPanel.setPreferredSize(JBUI.size(200, -1));

        this.filterTextField = new SearchTextField(true) {
            private Timer instantSearchTimer;
            private int instantSearchDelay = 350;

            @Override
            protected boolean preprocessEventForTextField(KeyEvent e) {
                if ((KeyEvent.VK_ENTER == e.getKeyCode()) || ('\n' == e.getKeyChar())) {
                    e.consume();
                    addCurrentTextToHistory();
                    postActionEvent();
                }else {
                    getInstantSearchTimer().restart();
                }
                return super.preprocessEventForTextField(e);
            }

            @Override
            protected void onFocusLost() {
                filterTextField.addCurrentTextToHistory();
                postActionEvent();
            }

            @Override
            protected void onFieldCleared() {
                postActionEvent();
            }

            public Timer getInstantSearchTimer() {
                if (this.instantSearchTimer == null) {
                    this.instantSearchTimer = new Timer(instantSearchDelay, new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            postActionEvent();
                        }
                    });
                    this.instantSearchTimer.setRepeats(false);
                }

                return this.instantSearchTimer;
            }

            public void postActionEvent() {
                this.getInstantSearchTimer().stop();
                perform();
            }

            public int getInstantSearchDelay() {
                return this.instantSearchDelay;
            }

            public void setInstantSearchDelay(int instantSearchDelay) {
                this.firePropertyChange("instantSearchDelay", this.instantSearchDelay, this.instantSearchDelay = instantSearchDelay);
            }
        };
        this.searchPanel.add(filterTextField, BorderLayout.CENTER);

        dbtableScrollPanel.setBorder(BorderFactory.createEmptyBorder());
        dbtablePanel.setBorder(new LineBorder(Color.LIGHT_GRAY));

        dbtableJTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = dbtableJTable.getSelectedRow();
                if (row < 0) {
                    return;
                }

                if (e.getClickCount() % 2 != 0) {
                    //单击事件
                    TableInfo tableInfo = getDbtableJTable().getTableInfoModel().getRowData(row);
                    if (dbtableClickConsumer != null) {
                        dbtableClickConsumer.accept(tableInfo);
                    }
                } else {
                    //双击事件

                }
            }
        });
    }

    public static void main(String[] args) {

    }

    public Consumer<TableInfo> getDbtableClickConsumer() {
        return dbtableClickConsumer;
    }

    public void setDbtableClickConsumer(Consumer<TableInfo> dbtableClickConsumer) {
        this.dbtableClickConsumer = dbtableClickConsumer;
    }

    public void resetContextInfo(List<TableInfo> tableInfoList) {
        filterTextField.setText("");
        this.tableInfoList = tableInfoList;
        getDbtableJTable().getTableInfoModel().reset(tableInfoList);
    }

    public void filterContextInfo(List<TableInfo> tableInfoList) {
        filterTextField.setText("");
        getDbtableJTable().getTableInfoModel().reset(tableInfoList);
    }



    private void perform(){
        if (tableInfoList == null){
            return;
        }

        String text = DbtableLeftPanelBuilder.this.filterTextField.getText();
        if (StringUtil.isBlank(text)) {
            filterContextInfo(tableInfoList);
        } else {
            String name = text.trim();
            getDbtableJTable().getTableInfoModel().filter(name);
        }
    }




    public JPanel getMyMainPanel() {
        return myMainPanel;
    }

    public DbtableInfoJTable getDbtableJTable() {
        return (DbtableInfoJTable) dbtableJTable;
    }

    private void createUIComponents() {
        this.dbtableJTable = new DbtableInfoJTable();
    }

}
